/**
 * Login Manager Process.
 */
function SOLoginManager() {
	this._host = QuickTuiUtils.exhost;
	
	this._login = Components.classes["@mozilla.org/login-manager;1"]
				  	.getService(Components.interfaces.nsILoginManager);
}

// methods
SOLoginManager.prototype = {
	
	// Remove password from Login Manager
	removePassword: function(user) {
		try {
			var logins = this._login.findLogins({}, this._host, "", null);
			for (var i=0; i < logins.length; i++) {
				if (logins[i].username == user) {
					this._login.removeLogin(logins[i]);
					QuickTuiUtils.log("Remove the user[" + user + "] password");
					break;
				}
			};
		} catch(e) {
			QuickTuiUtils.log("Can't remove user's password: " + e.message);
		}
	},
	
	removeAllPassword: function() {
		try {
			var logins = this._login.findLogins({}, this._host, "", null);
			for (var i=0; i < logins.length; i++) {
				this._login.removeLogin(logins[i]);
			};
			QuickTuiUtils.log("Remove all password at same host");
		} catch(e) {
			QuickTuiUtils.log("Can't remove all password at same host: " + e.message);
		}
	},
	
	// Save the user name and password to Login Manager
	savePassword: function(user, pass) {
		this.removeAllPassword();
		var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
		                                              Components.interfaces.nsILoginInfo,
		                                              "init");
		var loginInfo = new nsLoginInfo(this._host, 
										this._host + "/" + user,
										null,
										user,
										pass,
										"username",
										"password");
		this._login.addLogin(loginInfo);
		QuickTuiUtils.log("Add user[" + user + "] to Login Manager");
	},
	
	// Get user's password from Login Manager
	getPassword: function(user) {
		try {
			var logins = this._login.findLogins({}, this._host, "", null);
			
			for (var i=0; i < logins.length; i++) {
				if(logins[i].username == user) {
					QuickTuiUtils.log("Get the user [" + user + "] password");
					return logins[i].password;
				}
			}
		} catch(e) {
			QuickTuiUtils.log("Get user [" + user + "] password");
			return null;
		}
	},
	
	getAccounts: function() {
		QuickTuiUtils.log("Get all account");
		var accounts = [];
		var n = 0;
		
		try {
			var logins = this._login.findLogins({}, this._host, "", null);
			n = logins.length;
			
			for (var i=0; i < logins.length; i++) {
				accounts[logins[i].username] = logins[i].password;
			}
		} catch(e) {
			QuickTuiUtils.log("Can't retrieve password from Login Manager: " + e.message);
			return null;
		}
		QuickTuiUtils.log("Get extension[" + QuickTuiUtils.exname + "] all password");

		return n > 0 ? accounts : null;
	},

	// 取得当前在用的账户信息
	getCurrentAccounts: function() {
		var accounts = this.getAccounts();
		var result = {
			user: null,
			pass: null
		};
		
		for (var account in accounts) {
			result.user = account;
			result.pass = accounts[account];
			return result;
		}
		return null;
	}
}